package com.zhongan.common.dto;


import com.zhongan.common.enums.ErrorCode;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class ResultBase<T> implements Serializable {
    private static final long serialVersionUID = 2057948781441813066L;
    private boolean isSuccess = false;
    private String errorMsg = "";
    private String errorCode = "";
    private Integer pageNum = null;
    private Integer pageSize = null;
    private Integer totalNum =null;
    private Integer totalPage =null;
    private ErrorCode error = null;
    private T value;
    private Map<String, Object> additionalInfo = new HashMap<String, Object>();

    public ResultBase() {
        super();
    }

    public ResultBase(T value) {
        super();
        this.isSuccess = true;
        this.value = value;
    }

    public ResultBase(String errorMsg, String errorCode) {
        super();
        this.isSuccess = false;
        this.errorMsg = errorMsg;
        this.errorCode = errorCode;
    }

    public ResultBase(ErrorCode errorCode) {
        super();
        this.isSuccess = false;
        this.errorMsg = errorCode.getErrorMsg();
        this.errorCode = errorCode.getErrorCode();
    }


    public ResultBase(boolean success, String errorMsg, String errorCode) {
        super();
        this.isSuccess = success;
        this.errorMsg = errorMsg;
        this.errorCode = errorCode;
    }

    public  void setError(ErrorCode error){
        this.errorMsg = error.getErrorMsg();
        this.errorCode = error.getErrorCode();
        this.isSuccess = false;
    }

    public static <T> ResultBase<T> success()
    {
        ResultBase resultBase =  new ResultBase();
        resultBase.setSuccess(true);
        resultBase.setErrorMsg(ErrorCode.NORMAL.getErrorMsg());
        resultBase.setErrorCode(ErrorCode.NORMAL.getErrorCode());
        return resultBase;
    }

    public static  <T> ResultBase<T> success(Integer pageNum, Integer pageSize, Object value)
    {
        ResultBase resultBase =  new ResultBase();
        resultBase.setSuccess(true);
        resultBase.setPageNum(pageNum);
        resultBase.setPageSize(pageSize);
        resultBase.setValue(value);
        return resultBase;
    }

    public static   <T> ResultBase<T> success(Object value)
    {
        ResultBase resultBase =  new ResultBase();
        resultBase.setSuccess(true);
        resultBase.setValue(value);
        return resultBase;
    }

    public static  <T> ResultBase<T> fail(ErrorCode errorCode)
    {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(false);
        resultBase.setErrorCode(errorCode.getErrorCode());
        resultBase.setErrorMsg(errorCode.getErrorMsg());
        return resultBase;
    }
    public static  <T> ResultBase<T> fail(String errorMsg)
    {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(false);
        resultBase.setErrorMsg(errorMsg);
        return resultBase;
    }


    public static  <T> ResultBase<T> fail(String errorCode, String errorMsg)
    {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(false);
        resultBase.setErrorCode(errorCode);
        resultBase.setErrorMsg(errorMsg);
        return resultBase;
    }

    public static  <T> ResultBase<T> fail(String errorCode, String errorMsg, Object value)
    {
        ResultBase resultBase = new ResultBase();
        resultBase.setSuccess(false);
        resultBase.setErrorCode(errorCode);
        resultBase.setErrorMsg(errorMsg);
        resultBase.setValue(value);
        return resultBase;
    }

    public T getValue() {
        return this.value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public void appendAdditionalInfo(String key, Object value) {
        this.additionalInfo.put(key, value);
    }

    @SuppressWarnings("unchecked")
    //
    public <P> P getAdditionalInfo(String key) {
        return (P) additionalInfo.get(key);
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    
    public Integer getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(Integer totalNum) {
        this.totalNum = totalNum;
    }
    
    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("ResultBase [isSuccess=");
        builder.append(isSuccess);
        builder.append(", errorMsg=");
        builder.append(errorMsg);
        builder.append(", errorCode=");
        builder.append(errorCode);
        builder.append(", value=");
        builder.append(value);
        builder.append(", additionalInfo=");
        builder.append(additionalInfo);
        builder.append("]");
        return builder.toString();
    }
}
